package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.MemOrderDto;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

@RestController
//@RequestMapping("")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpServletRequest request;

    @PostMapping("/order/submit.do")
    public Result submit(@RequestBody MemOrderDto memOrderDto){
        if(memOrderDto==null){
            return new Result(false,"所传参数不合理");
        }

        Result result = orderService.submit(memOrderDto);
        return result;
    }

    @PostMapping("/validateCode/send4Order.do")
    public Result validateCode(String telephone){

        Integer code = new Random().nextInt(900000)+100000;
        //模拟发送6位数验证码

        //todo 根据手机号将验证码存储session中
        HttpSession session = request.getSession();
        session.setAttribute(telephone,code+"");
        System.out.println("获取的验证码为:"+code);
        return new Result(true,"模拟验证码存储成功");
    }
}
